package com.itqf.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/12/25
 * @Time: 上午10:35
 */
@Component
public class MyRouterFilter extends ZuulFilter {

    /**
     * 类型  pre(前)  post(后)  router(路由)  error(异常)
     * @return
     */
    @Override
    public String filterType() {
        return FilterConstants.ROUTE_TYPE;//路由过滤器
        //return "pre";
        }

    /**
     * 执行顺序
     * @return
     */
    @Override
    public int filterOrder() {
        return 1;
    }

    /**
     * 是否通过
     * @return
     */
    @Override
    public boolean shouldFilter() {

        boolean f = RequestContext.getCurrentContext().sendZuulResponse();
        System.out.println("MyRouterFilter:执行与否："+f);
        return f;
    }

    @Override
    public Object run() throws ZuulException {
        System.out.println("MyRouterFilter filter");
        //1,请求参数
        RequestContext context = RequestContext.getCurrentContext();
        String method = context.getRequest().getParameter("method");
        String serviceId = context.getRequest().getParameter("serviceId");
        // 静态路由 http://localhost:8889/服务名/方法名

        //动态路由：http://localhost:8889/?method=/api&serviceId=eureka-client-provider-v1
        //http://localhost:8889/?method=/findAll&serviceId=eureka-client-provider-v1
        //http://localhost:8889/?method=/find/1&serviceId=eureka-client-provider-v1

        //2.动态路由（跳转  访问）
        //serviceId
        //method---->redis   (serviceId+  method )
        //key  redis 查找 服务信息  方法信息  有则 访问  无则提示无api

        //serviceId  服务名
        //requestURI   方法的url  /api  /findAll /find/1

        context.put(FilterConstants.SERVICE_ID_KEY,serviceId);
        context.put(FilterConstants.REQUEST_URI_KEY,method);


        return null;
    }
}
